<?php
namespace app\admin\controller;
use mylib\Delimg;

class Team extends Init
{
    public function index()
    {
        $team = db('team')->select();
        $this->assign("team", $team);
        return view();
    }

    public function add(){
        if(request()->isPost()){
            $data['t_title'] = input('t_title');
            $data['t_name'] = input('t_name');

            $file = request()->file('image');
            $upload_path = config('upload.path');

            $info = $file->rule('uniqid')->move($upload_path);
            if($info){
                $savename = $info->getSaveName();
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
            $data['t_img'] = $savename;

            if(db('team')->insert($data)){
                $this->redirect('team/index');
            }else{
                return $this->error('添加失败！');
            }
        }
        return view();
    }

    public function edit(){
        $id = input('id');
        $team = db('team')->where('t_id',$id)->find();
        $this->assign("team", $team);

        if(request()->isPost()){
            $data['t_title'] = input('t_title');
            $data['t_name'] = input('t_name');

            //删除原图片
            $delimg = new Delimg();
            $delimg->delupimg('team','t_id',$id,'t_img',".jpg",'_150_200.jpg');

            //增加新的图片
            $file = request()->file('image');
            $upload_path = config('upload.path');

            $info = $file->rule('uniqid')->move($upload_path);
            if($info){
                $savename = $info->getSaveName();
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
            $data['t_img'] = $savename;


            if(db('team')->where('t_id',$id)->update($data)){
                $this->redirect('team/index');
//                return $this->success('修改成功！',url('informatioi/index'));
            }else{
                return $this->error('修改失败！');
            }
        }
        return view();
    }


    public function del(){
        $id = input('id');
        $delimg = new Delimg();
        $delimg->delupimg('team','t_id',$id,'t_img',".jpg",'_150_200.jpg');


        if(db('team')->where('t_id',$id)->delete()){
            $this->redirect('team/index');
        }else{
            return $this->error('删除失败！');
        }
    }

    public function delall(){
        $data = input('post.');

        $arrdel = $data['idarr'];
        $delimg = new Delimg();
        $delimg->delAllimg('team','t_id',$arrdel,'t_img',".jpg",'_150_200.jpg');

        if(request()->isPost()) {
            foreach ($arrdel as $id){
                db('team')->where('t_id',$id)->delete();
            }
            $this->redirect('team/index');
        }
    }


}